package com.zz.inventory.controller;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.exception.ServiceException;
import com.zz.inventory.domain.ExperimentScenariosWithPurchase;
import com.zz.inventory.feign.platformService;
import jakarta.servlet.http.HttpServletResponse;
import org.example.dto.CouponUsageDTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.ExperimentScenarios;
import com.zz.inventory.service.IExperimentScenariosService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验场景信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@RestController
@RequestMapping("/scenarios")
public class ExperimentScenariosController extends BaseController
{
    @Autowired
    private IExperimentScenariosService experimentScenariosService;
    @Autowired platformService platformService;     //远程调用

    /**
     * 查询实验场景信息列表
     *  调用平台管理模块，检查用户，是否激活优惠卷，激活成功，才可以看场景实验列表
     */
    @RequiresPermissions("Teacher:scenarios:list")
    @GetMapping("/list/{userids}")
    public TableDataInfo list(ExperimentScenarios experimentScenarios,
                              @PathVariable Long userids){
        //拿到用户ID，远程调用平台管理模块，拿ID查优惠卷记录,验证优惠卷状态
        CouponUsageDTo couponUsageDTo = new CouponUsageDTo();
        couponUsageDTo.setUserId(userids);
        AjaxResult result  = platformService.select(couponUsageDTo);
        if (result  == null || !"优惠卷已领取".equals(result.get("msg"))){
            TableDataInfo response = new TableDataInfo();
            response.setMsg("请先领取优惠卷,以查看实验场景列表");
            response.setRows(Collections.emptyList());
            return response;
        }
        //查询数据
            startPage();
            List<ExperimentScenarios> list = experimentScenariosService
                    .selectExperimentScenariosList(experimentScenarios);
            return getDataTable(list);

    }
    @GetMapping("/purchased/{schoolsId}")
    public AjaxResult getPurchasedScenarios(@PathVariable Long schoolsId) {
        List<ExperimentScenariosWithPurchase> list = experimentScenariosService.selectValidPurchasedScenariosBySchoolId(schoolsId);
        return AjaxResult.success(list);
    }
    /**
     * 导出实验场景信息列表
     */
    @RequiresPermissions("Teacher:scenarios:export")
    @Log(title = "实验场景信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentScenarios experimentScenarios)
    {
        List<ExperimentScenarios> list = experimentScenariosService.selectExperimentScenariosList(experimentScenarios);
        ExcelUtil<ExperimentScenarios> util = new ExcelUtil<ExperimentScenarios>(ExperimentScenarios.class);
        util.exportExcel(response, list, "实验场景信息数据");
    }

    /**
     * 获取实验场景信息详细信息
     */
    @RequiresPermissions("Teacher:scenarios:query")
    @GetMapping(value = "/{scenarioId}")
    public AjaxResult getInfo(@PathVariable("scenarioId") Long scenarioId)
    {
        return success(experimentScenariosService.selectExperimentScenariosByScenarioId(scenarioId));
    }

    /**
     * 新增实验场景信息
     */
    @RequiresPermissions("Teacher:scenarios:add")
    @Log(title = "实验场景信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentScenarios experimentScenarios)
    {
        return toAjax(experimentScenariosService.insertExperimentScenarios(experimentScenarios));
    }

    /**
     * 修改实验场景信息
     */
    @RequiresPermissions("Teacher:scenarios:edit")
    @Log(title = "实验场景信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentScenarios experimentScenarios)
    {
        return toAjax(experimentScenariosService.updateExperimentScenarios(experimentScenarios));
    }

    /**
     * 删除实验场景信息
     */
    @RequiresPermissions("Teacher:scenarios:remove")
    @Log(title = "实验场景信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scenarioIds}")
    public AjaxResult remove(@PathVariable Long[] scenarioIds)
    {
        return toAjax(experimentScenariosService.deleteExperimentScenariosByScenarioIds(scenarioIds));
    }
}
